package com.wut.clock.controller;

import com.wut.clock.aop.log.BusinessType;
import com.wut.clock.aop.log.Log;
import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.entity.Category;
import com.wut.clock.entity.Tag;
import com.wut.clock.service.ICategoryService;
import com.wut.clock.service.ITagService;
import com.wut.clock.vo.admin.CategoryVo;
import com.wut.clock.vo.admin.TagVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: yuwei
 * @Date: 2022/5/31 20:32
 * @Description: 标签和专栏管理
 */

@Api(value = "标签和专栏管理",tags = {"标签和专栏管理"})
@RestController
@RequestMapping("/tagCategory")
@RequiredArgsConstructor
public class TagCategoryController {

    private final ICategoryService categoryService;

    private final ITagService tagService;

    @ApiOperation("新增专栏")
    @PostMapping("/saveCategory")
    @Log(title = "新增专栏",businessType = BusinessType.INSERT)
    @RequiresPermissions("am:category:add")
    public CommonResult<String> saveCategory(@Valid @RequestBody Category category) {
        categoryService.saveCategory(category);
        return CommonResult.success();
    }

    @ApiOperation("删除专栏")
    @PostMapping("/deleteCategory/{categoryId}")
    @Log(title = "删除专栏",businessType = BusinessType.DELETE)
    @RequiresPermissions("am:category:delete")
    public CommonResult<String> deleteCategory(@PathVariable Integer categoryId) {
        categoryService.deleteCategory(categoryId);
        return CommonResult.success();
    }

    @ApiOperation("修改专栏")
    @PostMapping("/updateCategory")
    @Log(title = "修改专栏",businessType = BusinessType.UPDATE)
    @RequiresPermissions("am:category:update")
    public CommonResult<String> updateCategory(@Valid @RequestBody Category category) {
        categoryService.updateCategory(category);
        return CommonResult.success();
    }

    @ApiOperation("查看专栏列表")
    @GetMapping("/getCategories")
    @RequiresPermissions("am:category:query")
    public CommonResult<CommonPage<CategoryVo>> listCategories(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                               @RequestParam(value = "categoryName", required = false) String categoryName) {
        List<CategoryVo> categoryVos = categoryService.listCategories(pageNum, pageSize, categoryName);
        return CommonResult.success(CommonPage.restPage(categoryVos));
    }

    @ApiOperation("专栏列表下拉选择")
    @GetMapping("/getCategoriesForSelect")
    public CommonResult<List<Category>> listCategoriesForSelect() {
        List<Category> list = categoryService.listCategoriesForSelect();
        return CommonResult.success(list);
    }

    @ApiOperation("新增标签")
    @PostMapping("/saveTag")
    @Log(title = "新增标签",businessType = BusinessType.INSERT)
    @RequiresPermissions("am:tag:add")
    public CommonResult<String> saveTag(@Valid @RequestBody Tag tag) {
        tagService.saveTag(tag);
        return CommonResult.success();
    }

    @ApiOperation("删除标签")
    @PostMapping("/deleteTag/{tagId}")
    @Log(title = "删除标签",businessType = BusinessType.DELETE)
    @RequiresPermissions("am:tag:delete")
    public CommonResult<String> deleteTag(@PathVariable Integer tagId) {
        tagService.deleteTag(tagId);
        return CommonResult.success();
    }

    @ApiOperation("修改标签")
    @PostMapping("/updateTag")
    @Log(title = "修改标签",businessType = BusinessType.UPDATE)
    @RequiresPermissions("am:tag:update")
    public CommonResult<String> updateTag(@Valid @RequestBody Tag tag) {
        tagService.updateTag(tag);
        return CommonResult.success();
    }

    @ApiOperation("查看标签列表")
    @GetMapping("/getTags")
    @RequiresPermissions("am:tag:query")
    public CommonResult<CommonPage<TagVo>> listTags(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "tagName", required = false) String tagName) {
        List<TagVo> tagVos = tagService.listTags(pageNum, pageSize, tagName);
        return CommonResult.success(CommonPage.restPage(tagVos));
    }

    @ApiOperation("标签列表下拉选择")
    @GetMapping("/getTagsForSelect")
    public CommonResult<List<Tag>> listTagsForSelect() {
        List<Tag> list = tagService.listTagsForSelect();
        return CommonResult.success(list);
    }

}
